﻿using System;
using System.Collections.Generic;
using System.Text;
using INI_RW;
using System.IO;

namespace LuaBoard
{
    public static class Common
    {
        public static IniFile system_ini;
        public static string AppLocPath = "";
        public static bool AppLocEnable = false;
        public static string LuaBoardTitle = "";
        public static int CompressionLevel = 5;
        public static bool ShowDebugMsg = true;

        public static void setup(string path)
        {
            system_ini = new IniFile(Path.Combine(path, "setting.ini"));
            LuaBoardTitle = system_ini.IniReadValue("system", "LuaBoardTitle");
            if (system_ini.IniReadValue("system", "ShowDebugMessage")=="1")
                ShowDebugMsg = true;
            else
                ShowDebugMsg = false;

            int lv = 5;
            bool cl = int.TryParse(system_ini.IniReadValue("7zip", "CompressionLevel"), out lv);
            if (!cl)
                system_ini.IniWriteValue("7zip", "CompressionLevel", CompressionLevel.ToString());
            else
                CompressionLevel = lv;
        }
        public static void SetTitle(string title)
        {
            LuaBoardTitle = title;
            system_ini.IniWriteValue("system", "LuaBoardTitle", title);
        }
        public static void SetShowDebugMessage(bool debug)
        {
            ShowDebugMsg = debug;
            if (ShowDebugMsg)
                system_ini.IniWriteValue("system", "ShowDebugMessage", "1");
            else
                system_ini.IniWriteValue("system", "ShowDebugMessage", "0");
        }
        public static void SetCompressionLevel(int cl)
        {
            CompressionLevel = cl;
            system_ini.IniWriteValue("7zip", "CompressionLevel", CompressionLevel.ToString());
        }
        /// <summary>
        /// 获取路径2相对于路径1的相对路径
        /// </summary>
        /// <param name="strBasePath">路径1</param>
        /// <param name="strFullPath">路径2</param>
        /// <returns>返回路径2相对于路径1的路径</returns>
        /// <example>
        /// string strPath = GetRelativePath(@"C:\WINDOWS\system32", @"C:\WINDOWS\system\*.*" );
        /// //strPath == @"..\system\*.*"
        /// </example>
        public static string GetRelativePath(string strBasePath, string strFullPath)
        {
            strBasePath = System.IO.Path.GetFullPath(strBasePath);
            strFullPath = System.IO.Path.GetFullPath(strFullPath);

            if (!strBasePath.EndsWith("\\"))
                strBasePath += "\\";

            int intIndex = -1, intPos = strBasePath.IndexOf('\\');
            while (intPos >= 0)
            {
                intPos++;
                if (string.Compare(strBasePath, 0, strFullPath, 0, intPos, true) != 0)
                    break;

                intIndex = intPos;
                intPos = strBasePath.IndexOf('\\', intPos);
            }

            if (intIndex >= 0)
            {
                strFullPath = strFullPath.Substring(intIndex);
                intPos = strBasePath.IndexOf("\\", intIndex);
                while (intPos >= 0)
                {
                    strFullPath = "..\\" + strFullPath;
                    intPos = strBasePath.IndexOf("\\", intPos + 1);
                }
            }
            return strFullPath;
        }

        public static List<string> PathList(string FullPath)
        {
            string[] cutpath = FullPath.Split(new char[] { ':', '\\' });
            List<string> filter = new List<string>();
            foreach (string cp in cutpath)
            {
                filter.Add(cp);
            }
            return filter;
        }
    }
}
